using System;
using System.Collections.Generic;
using UnityEngine;

[Serializable]
public class StarterPackReward
{
	public StarterPackRewardGroup rewardGroup;

	public int amount;

	public MarketCategories category;

	public MarketSubcategories subCategory;

	public RareType rareType;

	private Reward _alternativeReward = new Reward();

	private bool alternativeRewardSet;

	private Product _lastProductResult;

	[NonSerialized]
	private StarterPack _starterPack;

	public Reward alternativeReward
	{
		get
		{
			return _alternativeReward;
		}
	}

	public Product lastProductResult
	{
		get
		{
			return _lastProductResult;
		}
		set
		{
			_lastProductResult = value;
		}
	}

	public StarterPack starterPack
	{
		get
		{
			return _starterPack;
		}
		set
		{
			_starterPack = value;
		}
	}

	public void GiveReward(Vector3 moneyPosition)
	{
		switch (rewardGroup)
		{
		case StarterPackRewardGroup.Cash:
			if (!settings.offlineMode && GameController.instance != null && GameController.instance.curTypeGame == TypeGame.normal)
			{
				settings.cathedExeptionsForFlurry -= amount;
			}
			settings.ShowSpecialLabelWithAnimation(amount, moneyPosition, false);
			break;
		case StarterPackRewardGroup.Gold:
			if (!settings.offlineMode && GameController.instance != null && GameController.instance.curTypeGame == TypeGame.normal)
			{
				settings.cathedWarningstForFlurry -= amount;
			}
			settings.ShowLabelWithAnimation(amount, moneyPosition, false);
			break;
		case StarterPackRewardGroup.Product:
		{
			Product product = GetProduct();
			if (product != null)
			{
				if (amount > 0)
				{
					MarketController.Instance.BuyProduct(product, true, amount);
				}
				else
				{
					MarketController.Instance.BuyProduct(product, true);
				}
			}
			else
			{
				_alternativeReward.GiveReward(moneyPosition);
			}
			break;
		}
		}
	}

	private bool ChekForDuplicates(Product rewardProduct)
	{
		foreach (StarterPackReward reward in starterPack.rewards)
		{
			if (reward != this && reward.rewardGroup == StarterPackRewardGroup.Product && reward.lastProductResult != null && reward.lastProductResult == rewardProduct)
			{
				return true;
			}
		}
		return false;
	}

	public Product GetProduct()
	{
		if (rewardGroup != StarterPackRewardGroup.Product)
		{
			lastProductResult = null;
			return null;
		}
		int num = 0;
		foreach (int tier in ProductDB.tiers)
		{
			if (tier > num && tier <= CupsManager.CurrentTier)
			{
				num = tier;
			}
		}
		if (CupsManager.CurrentLeagueIndex >= 8)
		{
			num = ProductDB.tiers[ProductDB.tiers.Count - 2];
		}
		Product product = TryGetProductWithSettings(category.ToString(), subCategory.ToString(), num, rareType);
		if (product != null)
		{
			lastProductResult = product;
			return product;
		}
		if (category.ToString() == "Weapons")
		{
			foreach (string subCategory in MarketController.Instance.ProductHierarchy.marketCategories.Find((MarketProductsHierarchy.MarketCategory c) => c.name.Equals(category.ToString())).subCategories)
			{
				if (subCategory != this.subCategory.ToString() && subCategory != "Gear")
				{
					product = TryGetProductWithSettings(category.ToString(), subCategory, num, rareType);
					if (product != null)
					{
						lastProductResult = product;
						return product;
					}
				}
			}
		}
		if (rareType - 1 >= RareType.Common)
		{
			product = TryGetProductWithSettings(category.ToString(), this.subCategory.ToString(), num, rareType - 1);
			if (product != null)
			{
				lastProductResult = product;
				return product;
			}
			if (category.ToString() == "Weapons")
			{
				foreach (string subCategory2 in MarketController.Instance.ProductHierarchy.marketCategories.Find((MarketProductsHierarchy.MarketCategory c) => c.name.Equals(category.ToString())).subCategories)
				{
					if (subCategory2 != this.subCategory.ToString() && subCategory2 != "Gear")
					{
						product = TryGetProductWithSettings(category.ToString(), subCategory2, num, rareType - 1);
						if (product != null)
						{
							lastProductResult = product;
							return product;
						}
					}
				}
			}
		}
		lastProductResult = null;
		return null;
	}

	private Product TryGetProductWithSettings(string category, string subcat, int tier, RareType rareType)
	{
		List<Product> list;
		if (subcat == "Tanks" || subcat == "Titans")
		{
			list = new List<Product>();
			for (int num = tier; num > 0; num--)
			{
				list.AddRange(MarketController.Instance.GetProductListForTier(category, subcat, num));
			}
		}
		else
		{
			list = MarketController.Instance.GetProductListForTier(category, subcat, tier);
		}
		if (list != null)
		{
			foreach (Product item in list)
			{
				if (item.rare == rareType && !alternativeRewardSet)
				{
					SetAlternativeRewardFromProduct(item);
				}
				if (item.rare == rareType && !item.Purchased && !ChekForDuplicates(item))
				{
					return item;
				}
			}
		}
		return null;
	}

	private void SetAlternativeRewardFromProduct(Product product)
	{
		if (product != null)
		{
			if (product.PriceIn == MoneyType.Cash)
			{
				_alternativeReward.rewardGroup = RewardGroups.Cash;
			}
			else
			{
				_alternativeReward.rewardGroup = RewardGroups.Coins;
			}
			if (product.MarketPrice > 0)
			{
				_alternativeReward.rewardAmount = product.MarketPrice;
				alternativeRewardSet = true;
			}
		}
	}
}
